<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-card class="search-wrapper">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="医院名称" prop="hospitalName">
          <el-input v-model="queryParams.hospitalName" placeholder="请输入医院名称" clearable />
        </el-form-item>
        <el-form-item label="医院等级" prop="hospitalLevel" style="width: 200px">
          <el-select v-model="queryParams.hospitalLevel" placeholder="请选择" clearable>
            <el-option label="三甲" value="三级甲等" />
            <el-option label="三乙" value="三级乙等" />
            <el-option label="二甲" value="二级甲等" />
            <el-option label="二乙" value="二级乙等" />
          </el-select>
        </el-form-item>
        <el-form-item label="所在城市" prop="city" style="width: 200px">
          <el-input v-model="queryParams.city" placeholder="请输入城市" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 列表区域 -->
    <el-card class="table-wrapper">
      <div class="toolbar">
        <el-button type="primary" @click="handleAdd">新增医院</el-button>
      </div>

      <el-table v-loading="loading" :data="hospitalList">
        <el-table-column type="index" label="序号" width="70" />
        <el-table-column prop="hospitalName" label="医院名称" />
        <el-table-column prop="hospitalLevel" label="医院等级">
          <template #default="{ row }">
            <el-tag v-if="row.hospitalLevel === '三级甲等'" type="success">三甲</el-tag>
            <el-tag v-else-if="row.hospitalLevel === '三级乙等'" type="warning">三乙</el-tag>
            <el-tag v-else>{{ row.hospitalLevel }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="hospitalType" label="医院类型" />
        <el-table-column prop="province" label="省份" />
        <el-table-column prop="city" label="城市" />
        <el-table-column prop="address" label="详细地址" show-overflow-tooltip />
        <el-table-column prop="phone" label="联系电话" />
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
            <el-button type="primary" link @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination v-model:current-page="queryParams.pageNum" v-model:page-size="queryParams.pageSize" :total="total"
        :page-sizes="[10, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogType === 'add' ? '新增医院' : '编辑医院'" width="600px">
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px">
        <el-form-item label="医院名称" prop="hospitalName">
          <el-input v-model="formData.hospitalName" placeholder="请输入医院名称" />
        </el-form-item>
        <el-form-item label="医院等级" prop="hospitalLevel">
          <el-select v-model="formData.hospitalLevel" placeholder="请选择医院等级">
            <el-option label="三甲" value="三级甲等" />
            <el-option label="三乙" value="三级乙等" />
            <el-option label="二甲" value="二级甲等" />
            <el-option label="二乙" value="二级乙等" />
          </el-select>
        </el-form-item>
        <el-form-item label="医院类型" prop="hospitalType">
          <el-input v-model="formData.hospitalType" placeholder="请输入医院类型" />
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-input v-model="formData.province" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="formData.city" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="formData.phone" placeholder="请输入联系电话" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { getHospitalList, getHospitalDetail, addHospital, updateHospital, deleteHospital } from '@/api/hospital'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  hospitalName: '',
  hospitalLevel: '',
  city: ''
})

const loading = ref(false)
const total = ref(0)
const hospitalList = ref([])
const dialogVisible = ref(false)
const dialogType = ref<'add' | 'edit'>('add')
const formRef = ref<FormInstance>()

// 表单数据
const formData = reactive({
  hospitalId: undefined,
  hospitalName: '',
  hospitalLevel: '',
  hospitalType: '',
  province: '',
  city: '',
  address: '',
  phone: ''
})

// 表单校验规则
const formRules = reactive<FormRules>({
  hospitalName: [
    { required: true, message: '请输入医院名称', trigger: 'blur' }
  ],
  hospitalLevel: [
    { required: true, message: '请选择医院等级', trigger: 'change' }
  ],
  hospitalType: [
    { required: true, message: '请输入医院类型', trigger: 'blur' }
  ],
  province: [
    { required: true, message: '请输入省份', trigger: 'blur' }
  ],
  city: [
    { required: true, message: '请输入城市', trigger: 'blur' }
  ],
  address: [
    { required: true, message: '请输入详细地址', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' }
  ]
})

// 获取医院列表
const getList = async () => {
  loading.value = true
  try {
    const res = await getHospitalList(queryParams)
    if (res.code == 200) {
      hospitalList.value = res.rows
      total.value = res.total
    }
  } catch (error) {
    console.error(error)
  }
  loading.value = false
}

// 查询
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置
const resetQuery = () => {
  queryParams.hospitalName = ''
  queryParams.hospitalLevel = ''
  queryParams.city = ''
  handleQuery()
}

// 新增
const handleAdd = () => {
  dialogType.value = 'add'
  dialogVisible.value = true
  formData.hospitalId = undefined
  formData.hospitalName = ''
  formData.hospitalLevel = ''
  formData.hospitalType = ''
  formData.province = ''
  formData.city = ''
  formData.address = ''
  formData.phone = ''
}

// 编辑
const handleEdit = async (row: any) => {
  dialogType.value = 'edit'
  dialogVisible.value = true
  const res = await getHospitalDetail(row.hospitalId)
  if (res.code == 200) {
    Object.assign(formData, res.data)
  }
}

// 删除
const handleDelete = (row: any) => {
  ElMessageBox.confirm('确认要删除该医院吗？', '提示', {
    type: 'warning'
  }).then(async () => {
    try {
      const res = await deleteHospital([row.hospitalId])
      if (res.code == 200) {
        ElMessage.success('删除成功')
        getList()
      }
    } catch (error) {
      console.error(error)
    }
  })
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return

  await formRef.value.validate()

  try {
    const submitFn = dialogType.value === 'add' ? addHospital : updateHospital
    const res = await submitFn(formData)
    if (res.code == 200) {
      ElMessage.success(dialogType.value === 'add' ? '新增成功' : '修改成功')
      dialogVisible.value = false
      getList()
    }
  } catch (error) {
    console.error(error)
  }
}

// 分页
const handleSizeChange = (val: number) => {
  queryParams.pageSize = val
  getList()
}

const handleCurrentChange = (val: number) => {
  queryParams.pageNum = val
  getList()
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;

  .search-wrapper {
    margin-bottom: 20px;
  }

  .table-wrapper {
    .toolbar {
      margin-bottom: 20px;
    }

    .el-pagination {
      margin-top: 20px;
      justify-content: flex-end;
    }
  }
}
</style>